//
//  ViewController.swift
//  BasicApp
//
//  Created by hungraim on 2021/7/28.
//

import UIKit

class BaseViewController: UIViewController, BaseVC {
    // MARK: - 布局
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white
        edgesForExtendedLayout = .all
        navigationController?.interactivePopGestureRecognizer?.isEnabled = true
        navigationController?.interactivePopGestureRecognizer?.delegate = self as UIGestureRecognizerDelegate
        
        if let viewControllers: [UIViewController] = navigationController?.viewControllers , viewControllers.count > 1{
            addDefaultBackBarButton()
        }
    }
    
    deinit {
        debugPrint("deinit--\(self)")
    }
    
    // MARK: - Navigation 关闭手势返回
    public func closePopGestureRecognizer() {
        let target = navigationController?.interactivePopGestureRecognizer?.delegate
        let pan = UIPanGestureRecognizer(target: target, action: nil)
        view.addGestureRecognizer(pan)
    }
    
    // MARK: - 返回方法
    @objc
    func goBack() {
        if let viewControllers: [UIViewController] = navigationController?.viewControllers {
            guard viewControllers.count <= 1 else {
                navigationController?.popViewController(animated: true)
                return
            }
        }
        
        if (presentingViewController != nil) {
            dismiss(animated: true, completion: nil)
        }
    }

    lazy var titleView: TextTitleView = {
        let v = TextTitleView.init(frame:CGRect(x: 0, y: 0, width: 180, height: 44), title:self.navigationItem.title ?? "", titleFont: UIFont.DinFont(ofSize: 18), titleColor: UIColor.black)
        return v
    }()
    public func addCustomTitleView() {
//        navigationItem.titleView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 180, height: 44))
        navigationItem.titleView = UIView.init(frame: .zero)
        navigationItem.titleView?.addSubview(titleView)
        titleView.title = self.navigationItem.title
    }
    /// 设置导航默认返回按钮
    public func addDefaultBackBarButton() {
        addLeftBarButton(normalImage: SwiftBrick.navBarNorBackImage ?? UIImage.init(named: "icon_nav_back"),
                         highLightImage: SwiftBrick.navBarHigBackImage ?? UIImage.init(named: "icon_nav_back")) { [weak self] _ in
            guard let `self` = self else{return}
            self.goBack()
        }
    }
    
    public func addLeftBarButton(_ button: UIButton...,
                                 fixSpace: CGFloat = 0,
                                 buttonSpace: CGFloat = 20){
        addLeftBarButtons(buttons: button, fixSpace: fixSpace)
    }
    
    fileprivate func addLeftBarButtons(buttons: [UIButton],
                                       fixSpace: CGFloat = 0,
                                       buttonSpace: CGFloat = 20){
        
        navigationItem.leftBarButtonItems?.removeAll()
        
        let gap = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target: nil, action: nil)
        gap.width = buttonSpace;
        
        var items = [UIBarButtonItem]()
        
        let count = buttons.count
        
        for (index, button) in buttons.enumerated() {
            button.contentHorizontalAlignment = .leading
            button.contentEdgeInsets = UIEdgeInsets(top: 0, left: fixSpace, bottom: 0, right: 0)
            if index != count - 1 {
                let barButton = UIBarButtonItem(customView: button)
                items.append(barButton)
                items.append(gap)
            }else{
                let barButton = UIBarButtonItem(customView: button)
                items.append(barButton)
            }
        }
        
        navigationItem.leftBarButtonItems = items
    }
    
    public func addRightBarButton(_ button: UIButton...,
                                  fixSpace: CGFloat = 0,
                                  buttonSpace: CGFloat = 20){
        addRightBarButtons(buttons: button, fixSpace: fixSpace)
    }
    
    public func addRightBarButtons(buttons: [UIButton],
                                        fixSpace: CGFloat = 0,
                                        buttonSpace: CGFloat = 20){
        
        navigationItem.rightBarButtonItems?.removeAll()
        
        let gap = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target: nil, action: nil)
        gap.width = buttonSpace;
        
        var items = [UIBarButtonItem]()
        
        let count = buttons.count
        
        for (index, button) in buttons.enumerated() {
            button.contentHorizontalAlignment = .trailing
            button.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: fixSpace)
            if index != count - 1 {
                let barButton = UIBarButtonItem(customView: button)
                items.append(barButton)
                items.append(gap)
            }else{
                let barButton = UIBarButtonItem(customView: button)
                items.append(barButton)
            }
        }
        
        navigationItem.rightBarButtonItems = items
    }
}

// MARK: - UIGestureRecognizerDelegate
extension BaseViewController: UIGestureRecognizerDelegate {
    // 接收到多个手势的代理方法
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        // 判断是否是侧滑相关手势
        if gestureRecognizer == navigationController?.interactivePopGestureRecognizer && gestureRecognizer.isKind(of: UIPanGestureRecognizer.self) {
            let pan = gestureRecognizer as! UIPanGestureRecognizer
            let point = pan.translation(in: view)
            // 如果是侧滑相关的手势，并且手势的方向是侧滑的方向就让多个手势共存
            if point.x > 0 {
                return true
            }
        }
        return false
    }
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer == navigationController?.interactivePopGestureRecognizer && navigationController?.viewControllers.count ?? 0 < 2 {
            return false
        }
        return true
    }
}
